// pages/myNian/myNian.js
var common = require("../../public.js");
// import pinyin from "wl-pinyin"
const app = getApp()
Page({

    /**
     * 页面的初始数据
     */
    data: {
      nianList:[],
      colors:'#99cca1'
    },

    click_detail(e){
      console.log(e.currentTarget.dataset.item)
      var id =e.currentTarget.dataset.item
      wx.navigateTo({
        url: '/pages/nianDetail/nianDetail?item='+ JSON.stringify(id)
      })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    randomHexColor() { //随机生成十六进制颜色
      var hex = Math.floor(Math.random() * 16777216).toString(16); //生成ffffff以内16进制数
      while (hex.length < 6) { //while循环判断hex位数，少于6位前面加0凑够6位
       hex = '0' + hex;
      }
       return '#' + hex; //返回‘#'开头16进制颜色
    
     },
    
    onLoad: function (options) {
      var that = this
      common.getLocalUserInfo(function (success, data) {
        if (success) {
          that.setData({
            userInfo: data.user,
            token: data.token
          }, function () {
            this.cardsList()
          })
        } else {
          that.setData({
            userInfo: null,
            token: '',
          })
        }
    
      })
     
    },
    // 时间戳
    time(time) {
      return common.dateFormat('yyyy-MM-dd HH:mm', time)
    },
    cardsList(){
      var that = this
      var arrCor = [
        'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/yearCard/hongYixiao.png',
        'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/yearCard/lvYixiao.png',
        'https://feiyangimage.oss-cn-shanghai.aliyuncs.com/wxApplets/yearCard/lanYixiao.png',
    ]
      var arr_three = []
        wx.request({
            url:'https://api.feiyanghy.com/userAnnualCard/getUserCardProductList',
            data: {userId:that.data.userInfo.id,
             token:that.data.token
            },
            method: "GET",
            header: {
              'content-type': 'application/x-www-form-urlencoded', // 默认值
            },
            success: function(res) {
              for(var i=0;i<res.data.data.length;i++){
                    for(var j =0;j<arrCor.length;j++){
                      arr_three.push(arrCor[j])
                    }    
                  }
                  var arrs= arr_three.slice(0,res.data.data.length)  
                  for(var i=0;i<res.data.data.length;i++){
                      res.data.data[i].colors=arrs[i]
                      res.data.data[i].validTime=that.time(res.data.data[i].validTime)
                     }
                     that.setData({
                      nianList:res.data.data
                     })
            }
        })
},
})